import Vue from 'vue'
import Router from 'vue-router'


import Index from "../views/index/index";
import Job from "../views/index/Job";
import Login from "../views/login/Login";
import {getToken} from "../util/token";
import Note from "../views/index/Note";


Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: "/",
      component: Index
    },
    {
        path:'/index',
        component:Index,
        meta:{
          check:true
        },
        children:[
          {
            path:"job",
            component:Job
          },
          {
            path:"note",
            component:Note
          }
        ]
    },
    {
      path: "/login",
      component: Login
    },

   ]}

)


// 前置守卫
router.beforeEach((to,from,next)=>{
  if(to.meta.check===true){
    let token=getToken()
    if(!token){
      alert("请您先登录")
       next("/login")
    }
  }
  next()
})

// 后置守卫
router.afterEach((to,from)=>{
  // console.log('to:',to)
  // console.log('from',from)
  let title=to.meta.title
  if(title===undefined){
    title="网站"
  }
  document.title=title
})


export default router
